package com.originskyseed.platform.job.jobhandler.cms;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.model.Cms;
import com.originskyseed.platform.cms.service.ICmsService;
import com.originskyseed.platform.core.job.biz.model.ReturnT;
import com.originskyseed.platform.core.job.handler.IJobHandler;
import com.originskyseed.platform.core.job.handler.annotation.JobHandler;
import com.originskyseed.platform.core.job.log.JobLogger;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.model.Anchor;
import com.originskyseed.platform.user.model.AnchorLevelLog;
import com.originskyseed.platform.user.model.SalesmanRelateLevelLog;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IAnchorLevelLogService;
import com.originskyseed.platform.user.service.IAnchorRelateLiveService;
import com.originskyseed.platform.user.service.IAnchorService;
import com.originskyseed.platform.user.service.ISalesmanLevelService;
import com.originskyseed.platform.user.service.ISalesmanRelateLevelLogService;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 全服通知公告
 *
 * @Author: wlj @date: 2021/7/6
 */

@Component
@JobHandler(value = "noticeJobsHandler")
public class NoticeJobsHandler extends IJobHandler {

	@DubboReference
	private IUserInfoService userInfoService;

	@DubboReference
	private IAnchorRelateLiveService anchorRelateLiveService;

	@DubboReference
	private ICmsService cmsService;

	@DubboReference
	private ISalesmanLevelService salesmanLevelaService;

	@DubboReference
	private ISalesmanRelateLevelLogService salesmanRelateLevelLogService;

	@DubboReference
	private IAnchorLevelLogService anchorLevelLogService;

	@DubboReference
	private IAnchorService anchorService;

	@Override
	public ReturnT<String> execute(String tenantCode) throws Exception {
		try {
			SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd");
			List<Cms> list = new ArrayList<>();
			// 业务员 查询等级
			List<SalesmanRelateLevelLog> salesmanRelateLevelLogList = this.salesmanRelateLevelLogService.selectListByNowTimeTenantCode(formatTime.format(new Date()), tenantCode);
			if (salesmanRelateLevelLogList != null && salesmanRelateLevelLogList.size() > 0) {
				for (SalesmanRelateLevelLog salesmanRelateLevelLog : salesmanRelateLevelLogList) {
					Cms cms = new Cms();
					String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
					// 查询名称
					UserInfo userInfo = this.userInfoService.selectByPrimaryKeyTenantCode(salesmanRelateLevelLog.getBusinessId(), tenantCode);
					cms.setId(key);
					Integer prority = this.cmsService.selectPrority();
					if (prority == null) {
						cms.setPrority(0);
					} else {
						cms.setPrority(prority + 1);
					}
					cms.setCreator("system");
					cms.setCreatorId("system");
					cms.setType(2);
					cms.setNoticeType(1);
					cms.setIsDel(2);
					cms.setIsTop(2);
					cms.setState(3);
					cms.setBelongPlatform("3");
					cms.setContent("恭喜" + userInfo.getName() + "达成业务员" + salesmanRelateLevelLog.getExpValue() + salesmanRelateLevelLog.getLevelName());
					list.add(cms);
				}
			}

			// 主播 查询等级
			List<AnchorLevelLog> anchorLevelLogList = this.anchorLevelLogService.selectListByNowTimeTenantCode(formatTime.format(new Date()), tenantCode);
			if (anchorLevelLogList != null && anchorLevelLogList.size() > 0) {
				Calendar now = Calendar.getInstance();
				for (AnchorLevelLog anchorLevelLog : anchorLevelLogList) {
					// 查询主播
					Anchor anchor = this.anchorService.selectByAnchorIdTenantCode(anchorLevelLog.getAnchorId(), tenantCode);
					Cms cms = new Cms();
					String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
					cms.setId(key);
					Integer prority = this.cmsService.selectPrority();
					if (prority == null) {
						cms.setPrority(0);
					} else {
						cms.setPrority(prority + 1);
					}
					cms.setCreator("system");
					cms.setCreatorId("system");
					cms.setType(2);
					cms.setNoticeType(1);
					cms.setIsDel(2);
					cms.setIsTop(2);
					cms.setState(3);
					cms.setBelongPlatform("2");
					cms.setContent("恭喜" + anchor.getAnchorNickName() + "晋升 " + (now.get(Calendar.MONTH) + 1) + " 月主播" + anchorLevelLog.getExpValue() + anchorLevelLog.getLevelName());
					list.add(cms);
				}
			}

			// 开放平台 查询新入职业务员
			List<UserInfo> userInfoList = this.userInfoService.selectUserByNowTimeTenantCode(formatTime.format(new Date()), tenantCode);
			if (userInfoList != null && userInfoList.size() > 0) {
				for (UserInfo userInfo : userInfoList) {
					Cms cms = new Cms();
					String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
					cms.setId(key);
					Integer prority = this.cmsService.selectProrityTenantCode(tenantCode);
					if (prority == null) {
						cms.setPrority(0);
					} else {
						cms.setPrority(prority + 1);
					}
					cms.setCreator("system");
					cms.setCreatorId("system");
					cms.setType(2);
					cms.setNoticeType(1);
					cms.setIsDel(2);
					cms.setIsTop(2);
					cms.setState(3);
					// this.cmsService.insert(cms);
					// 业务员
					if (userInfo.getType() == 6) {
						cms.setBelongPlatform("3");
						cms.setContent("热烈欢迎业务员" + userInfo.getName() + "加入我大公会");
					}
					// 主播
					if (userInfo.getType() == 11) {
						cms.setBelongPlatform("2");
						cms.setContent("热烈欢迎新晋主播" + userInfo.getName() + "加入我大家族");
					}
					list.add(cms);
				}
				// 开放平台
				for (UserInfo userInfo : userInfoList) {
					if (userInfo.getType() == 6) {
						Cms cms = new Cms();
						String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
						cms.setId(key);
						Integer prority = this.cmsService.selectProrityTenantCode(tenantCode);
						if (prority == null) {
							cms.setPrority(0);
						} else {
							cms.setPrority(prority + 1);
						}
						cms.setCreator("system");
						cms.setCreatorId("system");
						cms.setType(2);
						cms.setNoticeType(1);
						cms.setIsDel(2);
						cms.setIsTop(2);
						cms.setState(3);
						cms.setBelongPlatform("0");
						cms.setContent("热烈欢迎业务员" + userInfo.getName() + "入职");
						list.add(cms);
					}

				}

			}
			if (list != null && list.size() > 0) {
				this.cmsService.insertBatchTenantCode(list, tenantCode);
			}
		} catch (Exception e) {
			JobLogger.log("系统触发全服广播：" + getExceptionDetail(e));
			return FAIL;
		}
		return SUCCESS;
	}

	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 *
	 * @param ex
	 * @return
	 */
	public static String getExceptionDetail(Exception ex) {
		String ret = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PrintStream pout = new PrintStream(out);
		try {
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
		} catch (Exception e) {
		} finally {
			try {
				pout.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

}
